ENT.Type = "anim"
ENT.PrintName		= ""
ENT.Author			= ""
ENT.Contact			= ""
ENT.Purpose			= ""
ENT.Instructions	= ""

ENT.Spawnable			= false
ENT.AdminSpawnable		= false

VAR_NODE_INDEX		= "varNodeIndex"
VAR_TRACK_INDEX		= "varTrackIndex"
VAR_PLAYER_INDEX	= "varPlayerIndex"
VAR_NUM_CHILD_LINKS			= "varNumChildLinks"
VAR_NUM_CHILD_CONNECTIONS	= "varNumChildConnect"
VAR_NEW_CHILD_LINK_ENT		= "varNewChildLinkEnt"
VAR_NEW_CHILD_CONNECTION_ENT	= "varNewChildConnEnt"
VAR_HIGHLIGHTING	= "varHighlighting"
VAR_POSITION_LINK_ENDPNT	= "varConstEndPoint"
VAR_DODRAW_LINK_ENDPNT = "varConstShouldDraw"

function ENT:PrintError(code)
	print("********************")
	print("* ERROR")
	print("* Code: " .. code)
	print("* Please tell Gareth")
	return false
end

function ENT:IsValid(ent)
	if(ent && ent:IsValid()) then
		return true
	else
		return false
	end
end

function ENT:IsPathNode(ent)
	if not self:IsValid(ent) then
		return false
	end
	return ent:GetClass() == "node_actorpath"
end

function ENT:IsActor(ent)
	return (self:IsValid(ent) and ent:IsNPC())
end

function ENT:GetRootNode()

	local playerIndex = self:GetPlayerIndex()
	local trackIndex = self:GetTrackIndex()

	if CatmullRomCams.Tracks then
		if CatmullRomCams.Tracks[playerIndex] then 
			if CatmullRomCams.Tracks[playerIndex][trackIndex] then
				if CatmullRomCams.Tracks[playerIndex][trackIndex][1] then
					return CatmullRomCams.Tracks[playerIndex][trackIndex][1]
				end
			end
		end 
	end

	return nil
end

function ENT:AddEntityLink( child )

	if !(child && child:IsValid()) then
		print("Error: 'function ENT:AddEntityLink( child )'")
		return
	end

	self:SetNWEntity(VAR_NEW_CHILD_LINK_ENT,child)
end

function ENT:GetNodeIndex()
	return self:GetNWInt(VAR_NODE_INDEX)
end

function ENT:SetConstructPoint( drawState, position)
	self:SetNWBool(VAR_DODRAW_LINK_ENDPNT, drawState)
	self:SetNWVector(VAR_POSITION_LINK_ENDPNT, position)
end

function ENT:GetConstructPoint()
	local position = self:GetNWVector(VAR_POSITION_LINK_ENDPNT)
	local drawState = self:GetNWBool(VAR_DODRAW_LINK_ENDPNT)
	return drawState, position
end

function ENT:SetHighlighting( val )
	self:SetNWVector(VAR_HIGHLIGHTING, val)
end

function ENT:GetHighlighting()
	return self:GetNWInt(VAR_HIGHLIGHTING)
end

function ENT:GetTrackIndex()
	return self:GetNWString(VAR_TRACK_INDEX)
end

function ENT:GetPlayerIndex()
	return self:GetNWString(VAR_PLAYER_INDEX)
end

function ENT:AddEntityConnection( childEnt )

	if !(childEnt && childEnt:IsValid()) then
		print("Error: 'function ENT:AddEntityConnection( child )'")
		return
	end

	self:SetNWEntity(VAR_NUM_CHILD_CONNECTIONS,childEnt)
end

local NAME_OFFSET = Vector(0,0,30)
function ENT:DrawTrackName()
	if CLIENT then
		
		local offsetPos = self:GetPos() + NAME_OFFSET
		local pos = offsetPos:ToScreen()
		
		local distance = LocalPlayer():GetPos():Distance(offsetPos)

		

		local alpha = 0
		//math.Min(255,(255 * 500) / distance)

		if distance < 500 then
			alpha = 255
		elseif distance > 700 then
			alpha = 0
		else


			alpha = 255 * (1 - ((distance - 500) / (700 - 500)))

		end

		local black = Color( 176, 176, 176, alpha)
		local tipcol = Color( 46, 46, 46,  alpha)
		
		local nameVal = self:GetTrackIndex()
		local x = 0
		local y = 0
		local padding = 5
		
		surface.SetFont( "Default" )
		local w, h = surface.GetTextSize( nameVal )
		
		x = pos.x - (0.5 * w )
		y = pos.y - (0.5 * h )
		


		cam.Start2D()
		draw.RoundedBox( 1, x-padding, y-padding, w+padding*2, h+padding*2, tipcol )
				
		draw.NoTexture()
		surface.SetDrawColor( tipcol.r, tipcol.g, tipcol.b, tipcol.a )

		draw.DrawText(nameVal, "Default", x + w/2, y, black, TEXT_ALIGN_CENTER )
		cam.End2D()
	end
end

local NUMBER_WIDTH = 6
local NUMBER_OFFSET = Vector(0,0,15)
local NUMBER_HEIGHT = 6
function ENT:DrawNumber(val)

	if CLIENT then

		local firstDigit = nil
		local secondDigit = nil 
		local thirdDigit = nil

		if(val < 0) then
			val = -1 * val
			firstDigit = "13"	//negative symbol
		end

		if(val < 10) then

			if firstDigit then
				secondDigit = val
			else
				firstDigit = val
			end

		elseif(val == 10) then

			if(firstDigit)then
				secondDigit = "1"
				thirdDigit = "0"
			else
				firstDigit = "1"
				secondDigit = "0"
			end

		elseif(val > 10 && val < 100) then

			local modval = val % 10
			local unmodval = math.floor(val / 10) 

			if firstDigit then
				thirdDigit = ""..modval
				secondDigit = ""..unmodval
			else
				secondDigit = ""..modval
				firstDigit = ""..unmodval
			end

		end

		local labelDirection = Vector(1,0,0)
		local right = Vector(0,1,0)
		cam.Start3D2D(self:GetPos() + NUMBER_OFFSET, LocalPlayer():GetAngles(), 1)

			if firstDigit then
				render.SetMaterial( Material("sprites/key_" .. firstDigit))
				render.DrawQuadEasy( (right * NUMBER_WIDTH * 0), labelDirection, NUMBER_WIDTH, NUMBER_HEIGHT, Color(255,255,255,255),180)
			end

			if secondDigit then
				render.SetMaterial( Material("sprites/key_" .. secondDigit))
				render.DrawQuadEasy( (right * NUMBER_WIDTH * 1), labelDirection, NUMBER_WIDTH, NUMBER_HEIGHT, Color(255,255,255,255),180)
			end

			if thirdDigit then
				render.SetMaterial( Material("sprites/key_" .. thirdDigit))
				render.DrawQuadEasy( (right * NUMBER_WIDTH * 2), labelDirection, NUMBER_WIDTH, NUMBER_HEIGHT, Color(255,255,255,255),180)
			end

		cam.End3D2D()
	end
end